package by.grsu.epam.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import by.grsu.epam.domain.user.impl.Subscriber;
import by.grsu.epam.service.entitiesservice.SubscriberService;
import by.grsu.epam.service.entitiesservice.UserRoleService;

@Controller
public class WelcomeController {

	@Autowired
	private SubscriberService subscriberService;

	@Autowired
	private UserRoleService userRoleService;

	@RequestMapping("/")
	public ModelAndView index() {
		ModelAndView model = new ModelAndView("redirect:/welcome");
		
		return model;
	}

	// Welcome page
	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public ModelAndView welcome() {
		ModelAndView model = new ModelAndView("/welcome");
		
		return model;
	}

	// Sign in
	@RequestMapping(value = "/sign_in", method = RequestMethod.GET)
	public ModelAndView showSignIn() {
		ModelAndView model = new ModelAndView("/sign_in");

		return model;
	}

	// Sign up
	@RequestMapping(value = "/sign_up", method = RequestMethod.GET)
	public ModelAndView showSignUp() {
		ModelAndView model = new ModelAndView("/sign_up");

		Subscriber subscriber = new Subscriber();
		model.addObject("subscriber", subscriber);

		return model;
	}

	@RequestMapping(value = "/sign_up", method = RequestMethod.POST)
	public ModelAndView proccessSignUp(@ModelAttribute(value = "subscriber") Subscriber subscriber, BindingResult result) {
		ModelAndView model = new ModelAndView();

		if (result.hasErrors()) {
			model.setViewName("redirect:/sign_up");
		} else {
			model.setViewName("redirect:/sign_in");
			subscriberService.saveNewSubscriber(subscriber);
		}

		return model;
	}

	@RequestMapping(value = "/redirect", method = RequestMethod.GET)
	public ModelAndView redirect() {
		ModelAndView model = new ModelAndView("redirect:/signin");

		String username = getUsername();
		String role = userRoleService.findRoleByUsername(username);

		if ("ROLE_ADMINISTRATOR".equals(role)) {
			model.setViewName("redirect:/administrator/home");
		} else if ("ROLE_SUBSCRIBER".equals(role)) {
			model.setViewName("redirect:/subscriber/home");
		}

		return model;
	}

	private String getUsername() {
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

		String username;
		if (principal instanceof UserDetails) {
			username = ((UserDetails) principal).getUsername();
		} else {
			username = principal.toString();
		}
		return username;
	}
}
